package controllers

import "LoginTemplate/models"

type PaymentRecordsController struct {
	BaseController
}

type PaymentRecords struct {
	ID            int     `json:"id"`
	PaymentMethod int     `json:"payment_method"`
	UserId        int     `json:"user_id"`
	UserName      string  `json:"user_name"`
	PaymentType   int     `json:"payment_type"`
	PaymentTime   string  `json:"payment_time"`
	PaymentMoney  float64 `json:"payment_money"`
}

type PaymentRecordsData struct {
	Code        int              `json:"code"`
	Msg         string           `json:"msg"`
	Data        []PaymentRecords `json:"data"`
	Sum         int              `json:"sum"`         //总页数
	CurrentPage int              `json:"currentPage"` //当前页
	TotalNumber int              `json:"totalNumber"` //总条数
}

// 查询分类
func (u *PaymentRecordsController) SelectPaymentRecords() {
	var pictureList PictureList
	err := u.ShouldBind(&pictureList)
	var sum int
	var totalNumber int
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var client []PaymentRecords
	if pictureList.Id > 0 {
		sql := "select * from payment_records where id = ? limit ? , ?"
		tx := models.DB.Debug().Raw(sql, pictureList.Id, (pictureList.Page-1)*pictureList.Size, pictureList.Size).Scan(&client)
		sqlsum := "SELECT CEIL(COUNT(*) / ? ) FROM payment_records where id = ? "
		models.DB.Debug().Raw(sqlsum, pictureList.Id, pictureList.Size).Scan(&sum)
		sqlNumBer := "select count(*) from payment_records where id = ? "
		models.DB.Debug().Raw(sqlNumBer, pictureList.Id).Scan(&totalNumber)
		if tx.Error != nil {
			u.JsonMsg(500, "", "请求异常，请稍候再试")
		}
	} else {
		sql := "select * from payment_records limit ? , ?"
		tx := models.DB.Debug().Raw(sql, (pictureList.Page-1)*pictureList.Size, pictureList.Size).Scan(&client)
		sqlsum := "SELECT CEIL(COUNT(*) / ? ) FROM payment_records"
		models.DB.Debug().Raw(sqlsum, pictureList.Size).Scan(&sum)
		sqlNumBer := "select count(*) from payment_records"
		models.DB.Debug().Raw(sqlNumBer).Scan(&totalNumber)
		if tx.Error != nil {
			u.JsonMsg(500, "", "请求异常，请稍候再试")
		}
	}

	var picturedata PaymentRecordsData
	picturedata.Code = 200
	picturedata.Data = client
	picturedata.Msg = "成功"
	picturedata.Sum = sum
	picturedata.CurrentPage = pictureList.Page
	picturedata.TotalNumber = totalNumber
	//return picturedata
	u.JsonMsg(200, map[string]interface{}{
		"list":  picturedata,
		"count": "",
	}, "success")
}
